package 回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/subsets/?favorite=2cktkvj
 *
 * 题解：https://mp.weixin.qq.com/s?__biz=MzAxODQxMDM0Mw==&mid=2247493602&idx=1&sn=34b50382c660c33858d49e81583ce3be&chksm=9bd419eaaca390fc074042ac00658bd85ce4c1bd2c4de5d202936c3c32d7087064e1e57999d3&scene=178&cur_album_id=2165135739779448832#rd
 */
public class _78_子集 {

    List<Integer> result;

    List<List<Integer>> resultList;

    int[] nums;

    public List<List<Integer>> subsets(int[] nums) {
        result = new ArrayList<>();
        resultList = new ArrayList<>();
        if(nums == null || nums.length == 0) return resultList;
        this.nums = nums;

        dfs(0);
        return resultList;
    }


    private void dfs(int position) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            list.add(result.get(i));
        }
        resultList.add(list);

        for (int i = position; i < nums.length; i++) {
            result.add(nums[i]);
            dfs(i + 1);
            result.remove(result.size() - 1);
        }
    }
}
